package org.smallreunion.model;

import java.io.Serializable;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;

/**
 * BlobData
 *
 * byte[]形式で一時的にデータを保存したい場合に利用
 *
 * @author Sinmetal
 *
 */
@Model(schemaVersion = 1)
public class BlobData implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    Blob content;

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * @return content
     */
    public Blob getContent() {
        return content;
    }

    /**
     * @param content
     *            セットする content
     */
    public void setContent(Blob content) {
        this.content = content;
    }

    /**
     * RequestTokenを一時的に保存するためのKeyを作成
     *
     * @param token
     * @return
     */
    public static Key createRequestTokenKey(String token) {
        String keyString = "RequestToken=" + token;
        return Datastore.createKey(BlobData.class, keyString);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BlobData other = (BlobData) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
